<?php
// +----------------------------------------------------------------------
// | 青航系统 [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2023-永久 https://www.feifeiyu.club/ All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 青航-不吃芒果 <1716892803@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\exception\Jump;
use think\facade\View;
use think\File;
use app\admin\model\Upload as UploadModel;
class Attachment extends  Common{
    
    
    // 数据列表
    public function index() {
    	$where['fileext'] = request()->param('arr_p');
    	$where['originname'] = ['like',request()->param('searinfo')];
    	// 	文件的格式 转化为数组 再输出
    	$arr = ['png','jpg','jpeg','gif','zip','rar','mp3','mp4'];
    	$list = D('attachment')->where(formatWhere($where))->order('id DESC')->paginate(15);
        return view('attachment/index',['arr'=>$arr,'searinfo'=>$where['originname'],'arr_p'=>$where['fileext'],'list'=>$list]);
    }
    
    
    
    
    
    
    
    // 图片管理
    public function add() {
        $id = request()->param('name');
        $list = D('attachment')->order('id DESC')->paginate(12);
        return view('attachment/imagebox_add',['list'=>$list,'id'=>$id]);
    }
    
    
    
    
    
    
    
    // 删除数据
    public function dels() {
    	if (request()->isPost()) {
    	    // 判断是否获取到值
    		if(!input('param.fx'))Jump::error('没有选择项目!!');
    		// 再循环提交过来的值
    		foreach(input('param.fx') as $id) {
               $del = D('attachment')->where('id',$id)->value('originname');
               $this ->filename_del($del);
               
               $data = D('attachment')->where('id',$id)->delete();
            }
          }
    	// 判断是否执行成功
    	if($data) {
    		Jump::success("删除成功!!");
    	} else {
    	   Jump::error('删除失败!');
    	}
    }
    
    
    
    
    
    
    // 这里是删除上传的文件 源文件
    private function filename_del($data) {
        $filename = app()->getRootPath() . '/public/storage/'.$data.'';
    	if(file_exists($filename)) {
    		unlink($filename);
    	} else {
    		return false;
    	}
    }
    
    
    
    
    
    
   // 获取表单上传文件 例如上传了001.jpg 
   public function upload() {
    	if (request()->isPost()) {
    		$file = request()->file('file');
    		$site = get_config();
    		try {
                validate(['image'=>'fileSize:'.$site['upload_maxsize'].'|fileExt:'.$site['upload_types']])
                    ->check(['image' => $file]);
             $savename = \think\facade\Filesystem::disk('public')->putFile( 'topic', $file);
             
             UploadModel::insert_add($file,$savename);
                Jump::success("上传成功!!");
               
            } catch (\think\exception\ValidateException $e) {
                Jump::error($e->getMessage());
            }
    	}
    }
    
    
    
   //编辑器上传图片 
   public function upload_content(){
       if (request()->isPost()) {
    		$file = request()->file('file');
    		$site = get_config();
    		try {
                validate(['image'=>'fileSize:'.$site['upload_maxsize'].'|fileExt:'.$site['upload_types']])
                    ->check(['image' => $file]);
             $savename = \think\facade\Filesystem::disk('public')->putFile( 'topic', $file);
             
             UploadModel::insert_add($file,$savename);
             return json(['errno' => 0,'msg'=>'上传成功','data'=>['url'=>'/storage'.'/'.str_replace("\\",'/',$savename)]]);
               
            } catch (\think\exception\ValidateException $e) {
                Jump::error($e->getMessage());
            }
    	}
     }    
    
    
}